/**
 * 依赖生成器
 *
 * */
const spawn = require('cross-spawn');
const chalk = require('chalk');
const writeFile = require('../utils/FileWrite');
module.exports = class PackageManager {
    /**
     * @param options 参数信息
     * {
     *     name:  [必填项]
     *     version: 默认：0.1.0
     *     description: 默认 [create from @fans/cli]
     *     url: 仓库地址
     *     author: [必填]
     *     keywords: cli|demo 多个关键词以|分割
     *     publishConfig
     *     dependencies: {
     *
     *     }
     *     devDependencies: {
     *
     *     }
     *     engines: {
     *
     *     }
     * }
     * */
    constructor(options) {
        this.dependencies = null;
        this.devDependencies = null;
        this.default = {
            name: options.name || 'demo',
            version: options.version || '0.1.0',
            description: 'create form @fans/cli',
            repository: {
                type: "git",
                url: options.url,
            },
            author: options.author,
            keywords: options.keywords.split('|'),
            dependencies: {},
            devDependencies: {},
        }
        if (options.dependencies) {
            this.dependencies = options.dependencies;
        }
        if (options.devDependencies) {
            this.devDependencies = options.devDependencies;
        }
    }
    /**
     * 生成package.json文件
     * */
    async generatorPackage (dir) {
        console.log('dir', dir);
        await writeFile(dir, {
            'package.json': JSON.stringify(this.default, null, 4)
        })
    }

    /**
     * 安装依赖
     * @param name 安装对应的依赖，如果缺省安装所有依赖
     * */
    async install(name) {
        const tasks = [];
        if (this.dependencies) {
            tasks.push(spawn.sync('npm', ['i', '-S', ...this.dependencies], { stdio: 'inherit' }));
        }
        if (this.devDependencies) {
            tasks.push(spawn('npm', ['i', '-D', ...this.devDependencies], { stdio: 'inherit' }));
        }
        return Promise.all(tasks)
    }
    /**
     * 更新依赖
     * @param name 更新对应的依赖，如果缺省安装所有依赖
     * */
    update(name) {

    }
    /**
     * 卸载依赖
     * @param name 卸载的依赖名称 空的话不执行任何操作
     * */
    delete(name) {

    }
}
